package com.magic.smartcity.presenters;

import android.util.Log;

import androidx.annotation.NonNull;

import com.magic.smartcity.beans.NewsCategoryBean;
import com.magic.smartcity.beans.PressBean;
import com.magic.smartcity.beans.TocationBean;
import com.magic.smartcity.interfaces.INewsCallBack;
import com.magic.smartcity.interfaces.INewsModel;
import com.magic.smartcity.interfaces.INewsPresenter;
import com.magic.smartcity.model.NewsModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @description: 弱引用版本
 * @create: Create by Magic on 2022/2/2.
 */
public class NewsPresenter implements INewsPresenter<INewsCallBack> {
    private static final String TAG = NewsPresenter.class.getSimpleName();

    private static List<INewsCallBack> mINewsView = new ArrayList<>();

    INewsModel mNewsModel;

    @Inject
    public NewsPresenter(INewsModel newsModel) {
        mNewsModel = newsModel;
    }


    public void getNewList(int type) {
        if (mNewsModel != null) {
            Log.i(TAG, ":::::请求了新闻专栏数据");
            mNewsModel.getNewsList(type);
        }
    }

    public void getNewCategory() {
        if (mNewsModel != null) {
            Log.i(TAG, ":::::请求了新闻分类数据");
            mNewsModel.getNewsCategoryList();
        }
    }

    public void getNewCommentList() {

    }

    public void getRotationImages() {
        if (mNewsModel != null) {
            Log.i(TAG, ":::::请求了新闻轮播图数据");
            mNewsModel.getTocationImages();
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onMessageEvent(@NonNull NewsPresenterMessageEvent messageEvent) {
        if (messageEvent.getPressList() != null) {
            for (INewsCallBack calback : mINewsView) {
                calback.NewsLoaded(messageEvent.getPressList());
            }
        }
        if (messageEvent.getTocationList() != null) {
            for (INewsCallBack callBack : mINewsView) {
                callBack.NewsTocation(messageEvent.getTocationList());
            }
        }

        if (messageEvent.getCategoryBeanList() != null) {
            for (INewsCallBack callBack : mINewsView) {
                callBack.NewsCategoryLoaded(messageEvent.getCategoryBeanList());
            }
        }

    }


    public static class NewsPresenterMessageEvent {

        private List<PressBean.RowsBean> pressList;
        private List<TocationBean.RowsBean> tocationList;
        private List<NewsCategoryBean.DataBean> mCategoryBeanList;

        public List<NewsCategoryBean.DataBean> getCategoryBeanList() {
            return mCategoryBeanList;
        }

        public NewsPresenterMessageEvent setCategoryBeanList(List<NewsCategoryBean.DataBean> categoryBeanList) {
            mCategoryBeanList = categoryBeanList;
            return this;
        }

        public List<TocationBean.RowsBean> getTocationList() {
            return tocationList;
        }

        public NewsPresenterMessageEvent setTocationList(List<TocationBean.RowsBean> tocationList) {
            this.tocationList = tocationList;
            return this;
        }

        public List<PressBean.RowsBean> getPressList() {
            return pressList;
        }

        public NewsPresenterMessageEvent setPressList(List<PressBean.RowsBean> pressList) {
            this.pressList = pressList;
            return this;
        }
    }


    @Override
    public void registerCallBack(INewsCallBack callback) {
        if (!mINewsView.contains(callback)) {
            mINewsView.add(callback);
            Log.d(TAG, "::::::注册了" + callback);
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void unRegisterCallBack(INewsCallBack callback) {
        Log.d(TAG, "::::::取消注册了" + callback);
        EventBus.getDefault().unregister(this);
        mINewsView.remove(callback);
    }
}
